/*
 * =====================================================================================
 *
 *       Filename:  cl_buffer.hxx
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  04/05/2011 12:17:50 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Chen Ken (Ken), zlchen.ken@gmail.com
 *        Company:  EMC
 *
 * =====================================================================================
 */

#ifndef _CL_BUFFER_HXX_
#define _CL_BUFFER_HXX_

#include <vector>
#include <string>

namespace neo
{
class CLBuffer
{
private:
  static const size_t BUF_HEAD_RES_SIZE = 32;
  static const size_t BUF_INIT_SIZE = 1024 - 32;

public:
  CLBuffer();
  size_t readableBytes(void) const;
  size_t writableBytes(void) const;
  size_t discardableBytes(void) const;
  void write(const std::string& data);
  void write(const void* data, size_t siz);
  size_t read(std::string& data, size_t siz = 0);
  size_t read(void* data, size_t siz);
  ssize_t readfd(int fd, size_t siz = 0);
  ssize_t writefd(int fd, size_t siz = 0);
  ssize_t writefd(int fd, const void* data, size_t siz);
  void reset(void);
  const void* readableBegin(void) const;
  size_t size(void) const;

private:
  void* writableBegin(void);
  void grow(size_t siz);

private:
  std::vector<char> m_buf;
  size_t m_readerIndex; 
  size_t m_writerIndex;
};

} // neo

#endif // _CL_BUFFER_HXX_

